<template>
  <div class="editBox">
    <el-button size="mini" @click="handleEdit()">编辑</el-button>
    <el-dialog :show-close="false" destroy-on-close :modal-append-to-body="false" title="提示" :visible="dialogVisible" width="30%">
      <el-form label-position="right" ref="formName" label-width="80px" :model="formLabelAlign">
        <el-form-item label="学校">
          <el-select v-model="formLabelAlign.schoolId" placeholder="请选择">
            <el-option v-for=" item,index in $store.state.school.profile" :key="item.schoolId" :label="item.schoolName" :value="item.schoolId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教师">
          <el-select v-model="formLabelAlign.teacherId" remote :remote-method="searchUserNameFn" filterable placeholder="可搜索">
            <el-option v-for="item in teacherList" :key="item.userId" :label="item.username" :value="item.userId">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="课程名">
          <el-input v-model="formLabelAlign.coursename"></el-input>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="formLabelAlign.subjectId" placeholder="请选择">
            <el-option v-for=" item,index in subjectList" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考试时间">
          <el-date-picker v-model="formLabelAlign.examtime" value-format="yyyy-MM-dd HH:mm:SS" type="datetime" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="mooc地址">
          <el-input v-model="formLabelAlign.moocLink"></el-input>
        </el-form-item>
        <el-form-item label="课程描述">
          <el-input :rows="4" type="textarea" v-model="formLabelAlign.coursedesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { update } from '@/api/course'
import { searchUserName } from '@/api/user'
import debounce from '@/utils/debounce'
export default {
  props: {
    info: {
      type: Object,
      default: () => ({})
    },
    subjectList: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      dialogVisible: false,
      formLabelAlign: {
        schoolId: '',
        examtime: '',
        coursedesc: '',
        teacherId: '',
        subjectId: '',
        moocLink: '',
        coursename: '',
        courseId: ''
      },
      teacherList: []
    }
  },
  mounted() {
    const { schoolId, examtime, coursedesc, teacherId, subjectId, moocLink, coursename, courseId } = this.info
    console.log(schoolId, examtime, coursedesc, teacherId, subjectId, moocLink, coursename, courseId)
    const data = { schoolId, examtime, coursedesc, teacherId, subjectId, moocLink, coursename, courseId }
    this.formLabelAlign = data
  },
  methods: {
    searchUserNameFn(username) {
      if (username === '') {
        return
      }
      debounce(
        searchUserName(username).then(res => {
          this.teacherList = res.data
        }),
        1000
      )
    },
    handleEdit() {
      this.dialogVisible = true
    },
    submit() {
      this.dialogVisible = false
      this.editScoreFn()
    },
    editScoreFn() {
      update(this.formLabelAlign).then(res => {
        this.$message({
          type: 'success',
          message: res.msg
        })
        this.$emit('editeChange')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.editBox {
  padding-right: 10px;
}
</style>